import React, {useEffect, useState} from "react";
import {Form, Input, message, Modal, Select} from "antd";
import {FormComponentProps} from "antd/es/form";
import api from '@/services'
import styles from '../index.less'
import {getAccessToken} from '@/utils/authority';

interface Props extends FormComponentProps {
  data?: any,
  close: Function,
  save: Function
}
// interface State {
//   tenantList: any;
//   screenList: any;
// }
const Save: React.FC<Props> = props => {
  const { form, form: { getFieldDecorator } } = props;
  // const initState: State = {
  //   tenantList: [],
  //   screenList: [],
  // };
  const [imgList, setImgList] = useState([]);

  const save = () => {
    form.validateFields((err, fileValue) => {
      if (err) return;
      console.log('fileValue',fileValue)
      console.log('tenantList', tenantList)
      if(fileValue.tenantId == 'all'){
        fileValue.tenantName = 'all'
      }else if(fileValue.tenantId == 'admin'){
        fileValue.tenantName = 'admin'
      }else{ 
        for(let i = 0; i < tenantList.length; i++){
          if(tenantList[i].id == fileValue.tenantId){
            fileValue.tenantName = tenantList[i].name
            continue
          }
        }
      }
      api.menu.saveIframe(fileValue).then(res => {
          if (res.status === 200) {
            message.success("保存成功");
            props.save()
          }
      })
    })
  };

  const onClick = (value: any[]) => {
    console.log('value',value)
    props.save(value)
  };

  useEffect(() => {
    api.workorder.getImg({}).then(res => {
      if (res.status === 200) {

        setImgList(res.result);
        const list  = [
          {
            "id": 1,
            "imgUrl": 'http://121.37.145.72:8867/bending/17.png'
          },
          {
            "id": 2,
            "imgUrl": 'http://121.37.145.72:8867/bending/17.png'
          },
          {
            "id": 3,
            "imgUrl": 'http://121.37.145.72:8867/bending/17.png'
          },
        ]
        // setImgList(list)
      }
    })
  }, []);
  return (
    <Modal
      visible
      title={'选择图片'}
      onCancel={() => props.close()}
      onOk={() => {
        save()
      }}
      footer={
        [] // 设置footer为空，去掉 取消 确定默认按钮
      }
    >
      <div style={{height: 400,overflow: 'auto'}}>
        <div className={styles.getwayCard}>
          {imgList.map((item: any, index: number) => 
            <div className={styles.getwayItem} style={index/5 == 1 || index == 0 ? {marginTop: 20} : {marginTop: 20, marginBottom: 20}} onClick={() => {
              onClick(item);
            }}>
              <img src={item.imgUrl} width="100" />
            </div>  
          )}
        </div>
      </div>
    </Modal>
  )
};
export default Form.create<Props>()(Save);
